$(document).ready(function(){
    //下订单
    $(".orderSub").click(function(){


        diliver = $("input[type='radio']:checked").val();;


        if(
            (document.getElementById("diliver").checked)
            ||
            (document.getElementById("nodiliver").checked)
        )
        {



        /*订单中有勾选的话情况*/
        if($(".checkOrder").is(':checked')==true){
            var balance = parseFloat($(".balance").val());
            var order_price = parseFloat($(".order_price").val());
            /*如果订单金额小于余额，可以正常下单*/
            if(order_price <= balance){
                var checkedLen = $(".checkboxT .checkOrder:checked").length;
                var n = 0;

                //开始循环插入勾选的菜式数据
                for(var i=0;i<checkedLen;i++){


                    //准备好要插入的数据
                    var orderOnePrice = $(".checkboxT").eq(i).find(".checkOrder").attr("data-price");
                    var productId = $(".checkboxT").eq(i).find(".checkOrder").attr("data-id");
                    var date = $(".checkboxT").eq(i).find(".checkOrder").attr("data-date");
                    var date2 = $(".checkboxT").eq(i).find(".checkOrder").attr("data-day");
                    var orderNum = $(".checkboxT").eq(i).find(".orderNum").val();

                    for(var _i=0;_i<orderNum;_i++) {
                        //异步插入数据到数据库
                        $.ajax({
                            type: "POST",
                            url: "coresection/order-detail.php",
                            data: "post_price=" + orderOnePrice + "&post_id=" + productId + "&post_date=" + date + "&post_date2=" + date2 + "&post_num=" + 1+ "&diliver=" + diliver,
                            success: function () {
                                n++;
                                if (n == checkedLen) {
                                    alert("下订已完成，谢谢惠顾！");
                                    document.location = "order.php";
                                }

                            }
                        })
                        //异步插入数据到数据库

                    }
                }
            }else{
                alert("余额不足，请及时充值！");
            }
        }else{
            alert("请勾选要订的请勾选要订的套餐！");
        }



        }
        else
        {
            alert("请选择就餐地点！");
        }


    });








    // 展示每天产品列表
    $(".order_list_ol li").click(function(){
        $(".order_list_ol li").removeClass("active");
        $(this).addClass("active");
        $(".orderPrice").html("0");
        $(".order_price").val("0");
        var $class = $(this).attr("class");
        var $dataDate = $(this).attr("data-date");
        var $dataDate2 = $(this).attr("data-date2");
        var $dataDate3 = $(this).attr("data-date3");
        var $classWeek=$class.substr(5,1);
        var $classAt=$class.substr(7,1);
        switch($classAt){
            case "1":
                var $classAtStr = "早餐";
                break;
            case "2":
                var $classAtStr = "午餐";
                break;
            case "3":
                var $classAtStr = "晚餐";
                break;
        }
        $.ajax({
            type: 'POST',
            data:"week="+$classWeek+"&at="+$classAt+"&date="+$dataDate+"&date2="+$dataDate2+"&date3="+$dataDate3+"&atStr="+$classAtStr+"&class="+$class,
            url:"coresection/product-week.php",
            dataType: 'html',
            success: function(data){
                $(".center_area").html(data);
//                orderPriceAdd();
            }
        });
    });



    // 产品列表总价结算

    $(".center_area").on('click','.checkOrder',function(){
        checkPrice($(this));
    });





    function checkPrice(obj){
        if(obj.is(':checked')==true){
            var addPrice = obj.attr("data-price")*obj.attr("data-num");
            obj.parents(".product_list_table").addClass("checkboxT");

            var redubutton=document.getElementById("redunum"+obj.attr("data-id"));
            var addbutton=document.getElementById("addnum"+obj.attr("data-id"));

            addbutton.style.display="none";
            redubutton.style.display="none";

            //addnum.setAttribute('style', 'display: none !important');


            orderPrice(addPrice);
        }else{

            var addPrice = obj.attr("data-price")*obj.attr("data-num");
            obj.parents(".product_list_table").removeClass("checkboxT");

            var redubutton=document.getElementById("redunum"+obj.attr("data-id"));
            var addbutton=document.getElementById("addnum"+obj.attr("data-id"));

            addbutton.style.display="block";
            redubutton.style.display="block";

            orderPrice(-addPrice);
        }
    }



    function orderPrice(p){
        var orderPrice = $(".order_price").val();
        orderPrice = parseFloat(orderPrice) + parseFloat(p);
        $(".orderPrice").html(orderPrice);
        $(".order_price").val(orderPrice);
    }








    // 退订
    $(".tuidingBtn").on("click",function(){
        var orderID = $(this).val();
        var productID = $(this).attr("data-value");
        var orderPrice = $(this).attr("data-price");
        var productDate = $(this).attr("data-date");
        $.ajax({
            type:"POST",
            url:"coresection/tuiding.php",
            data:"orderID="+orderID+"&productID="+productID+"&orderPrice="+orderPrice+"&productDate="+productDate,
            dataType:"text",
            success:function(msg){
                alert(msg);
                document.location = "setup_summary.php";
            }
        })
    });



    // 批量退订
    $(".fulltuidingBtn").on("click",function(){


        var id = document.getElementsByName('mycheckbox');

        var values = new Array();
        var strs = new Array();



        for(var i = 0; i < id.length; i++){
            if(id[i].checked){

                values.push(id[i].value);
                strs[i]=id[i].value.split("@"); //字符分割

                orderID = strs[i][0];
                productID = strs[i][1];
                orderPrice = strs[i][2];
                productDate = strs[i][3];


                $.ajax({
                    type:"POST",
                    url:"coresection/tuiding.php",
                    data:"orderID="+orderID+"&productID="+productID+"&orderPrice="+orderPrice+"&productDate="+productDate,
                    dataType:"text",
                    success:function(msg){
                        alert(msg);

                    }
                })



            }


            document.location = "setup_summary_message.php";


        }



        alert("退订完毕.");


    });




    // 查询订单默认当前月
    //var d = new Date();
    //var vMon = d.getMonth() + 1;
    //$(".month_"+vMon).attr("selected","selected");



    //打印部分
    $(".print_btn").click(function(){
        $(".printArea").printArea();
    });




    //后台总价计算
    var $len = $(".orderPriceTd").length;
    var $priceTd = 0;
    var $caiNum = new Array();
    var $caiNumNew = new Array();
    for(var i=0;i<$len;i++){
        var $onePriceTd = parseFloat($(".orderPriceTd").eq(i).html());
        $priceTd = $priceTd + $onePriceTd;
        $caiNum.push($(".productId").eq(i).attr('data-id')); //循环统计菜色id放入数组
    }

    for(var n=0;n<$caiNum.length;n++) { //剔除相同的id项
        var items=$caiNum[n];
        if($.inArray(items,$caiNumNew)==-1) {
            $caiNumNew.push(items);
        }
    }

    $priceTd = $priceTd.toFixed(2);
    $(".orderPriceB").html($priceTd);

    //后台查询当天菜色统计数量
    for(var t=0;t<$caiNumNew.length;t++){ //循环数组里面的菜色id，统计其id的条数
        var $caiStr = $("."+$caiNumNew[t]);
        var html = "<li class='"+$caiNumNew[t]+"'>"+$caiStr.html()+"<b>"+$caiStr.length+"</b>份</li>";
        $(".caise").append(html);
    }


});




var productinfo={
    plus:function(id){
        var num=Number($(id).parent().find('span').text());

        var nownum = num+1;
        $(id).parent().find('span').text(nownum);//数量
        $(id).parent().find('input').attr('value',nownum);//改变input框的值
        $(id).parent().parent().parent().find('input').attr('data-num',nownum);//改变input框的值

        return false;
    },
    reduce:function(id){
        var num=Number($(id).parent().find('span').text());
        if(num>1){
            var nownum = num-1;
            $(id).parent().find('span').text(nownum);//数量
            $(id).parent().find('input').attr('value',nownum);//改变input框的值 
            $(id).parent().parent().parent().find('input').attr('data-num',nownum);//改变input框的值
        }

        return false;
    }
}





function checkAll(obj){
    $("#summary_table input[type='checkbox']").prop('checked', $(obj).prop('checked'));
}